package com.wangdong.wdutils.utils;


import com.wangdong.wdutils.logger.DLAndroidLogAdapter;
import com.wangdong.wdutils.logger.DLCsvDLFormatStrategy;
import com.wangdong.wdutils.logger.DLDiskLogAdapter;
import com.wangdong.wdutils.logger.DLLogger;
import com.wangdong.wdutils.logger.DLPrettyFormatStrategy;

/**
 * @author luyz
 * Log统一管理类
 */
public class DLLogUtil {
	
	private static boolean isDebug = true;// 是否需要打印bug，可以在application的onCreate函数里面初始化
	private static final String TAG = "DLLog";

	private DLLogUtil() {
		/* cannot be instantiated */
		throw new UnsupportedOperationException("cannot be instantiated");
	}

	public static void setDebug(boolean debug){

		isDebug = debug;
		DLLogger.addLogAdapter(new DLAndroidLogAdapter(DLPrettyFormatStrategy.newBuilder().tag(TAG).methodCount(5).build()));
	}

	public static void setWriteLogToFile(boolean wltf){

		DLLogger.clearLogAdapters();
		if (isDebug){
			DLLogger.addLogAdapter(new DLAndroidLogAdapter(DLPrettyFormatStrategy.newBuilder().tag(TAG).methodCount(5).build()));
		}
		if (wltf) {
			String tempFolder = DLFolderManager.getLogsFolder().getAbsolutePath();
			DLLogger.addLogAdapter(new DLDiskLogAdapter(DLCsvDLFormatStrategy.newBuilder().tag(TAG).saveFolder(tempFolder).build()));
		}
	}

	public static void p(String msg,Object... args){
		log("p",msg,args);
	}

	// 下面四个是默认tag的函数
	public static void i(String msg,Object... args) {
		log("i",msg,args);
	}

	public static void d(String msg,Object... args) {
		log("d",msg,args);
	}

	public static void e(String msg,Object... args) {
		log("e",msg,args);
	}

	public static void v(String msg,Object... args) {
		log("v",msg,args);
	}

	public static void w(String msg,Object... args) {
		log("w",msg,args);
	}

	public static void wtf(String msg,Object... args) {
		log("wtf",msg,args);
	}


	public static void d(Object o){
		if (isDebug) {
			DLLogger.d(o);
		}
	}

	public static void json(String json) {
		if (isDebug) {
			DLLogger.json(json);
		}
	}

	public static void xml(String xml) {
		if (isDebug) {
			DLLogger.xml(xml);
		}
	}

	public static void printErrorMessage(Exception e) {
		if (isDebug)
			e.printStackTrace();
	}

	public static void log(String type,String msg,Object... args){
		if (isDebug){
			switch (type) {
				case "p":
				case "w":
					DLLogger.w(msg, args);
					break;
				case "d":
					DLLogger.d(msg, args);
					break;
				case "i":
					DLLogger.i(msg, args);
					break;
				case "e":
					DLLogger.e(msg, args);
					break;
				case "v":
					DLLogger.v(msg, args);
					break;
				default:
					DLLogger.wtf(msg, args);
					break;
			}
		}
	}
}
